package controllers



import (
	"newAi/new_web/models"
	"strings"
)

type SeatTaskController struct {
	BaseController
}

func (self *SeatTaskController) List() {
	self.Data["pageTitle"] = "座席任务"
	self.display()
}

func (self *SeatTaskController) Add() {
	if self.isOrg == true {
		self.SetUserGroup(self.userId)
	}
	if self.userId != 1 {
		self.SetAllGroup1(self.userId)
	} else {
		self.SetAllGroup()
	}
	self.Data["pageTitle"] = "座席任务添加"
	self.display()
}

func (self *SeatTaskController) Edit() {
	self.Data["pageTitle"] = "编辑任务"
	if self.isOrg == true {
		self.SetUserGroup(self.userId)
	}
	if self.userId != 1 {
		self.SetAllGroup1(self.userId)
	} else {
		self.SetAllGroup()
	}
	id, _ := self.GetInt("id", 0)
	AiTask, _ := models.AiSeatTaskGetById(id)
	row := make(map[string]interface{})
	row["id"] = AiTask.Id
	row["tk_name"] = AiTask.Tk_name
	row["tk_type"] = AiTask.Tk_type
	row["tk_state"] = AiTask.Tk_state
	row["tk_start_time"] = self.FormatTime(AiTask.Tk_start_time)
	row["tk_end_time"] = self.FormatTime(AiTask.Tk_end_time)
	row["tk_owner"] = AiTask.Tk_owner
	row["tk_gateway_group"] = AiTask.Tk_gateway_group
	row["tk_num_group"] = AiTask.Tk_num_group
	self.Data["task"] = row
	self.display()

}

func (self *SeatTaskController) Table() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//group 列表
	num_group, err := models.NumberGroupIdName()
	self.CheckError(err)
	gw_group, err := models.GatewayGroupIdName()
	self.CheckError(err)
	user_group, err := models.UserGroupIdName()
	self.CheckError(err)
	//任务状态
	tk_state := []string{"未开始", "进行中", "已经结束", "暂停"}
	//查询条件
	filters := make([]interface{}, 0)
	if self.isOrg == true {
		filters = append(filters, "tk_owner", self.userId)
	}
	result, count := models.AiSeatTaskGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["tk_name"] = v.Tk_name
		if v.Tk_type == 0 {
			row["tk_type"] = "及时任务"
		} else {
			row["tk_type"] = "定时任务"
		}
		row["tk_state"] = tk_state[v.Tk_state]
		row["tk_start_time"] = self.FormatTime(v.Tk_start_time)
		row["tk_end_time"] = self.FormatTime(v.Tk_end_time)
		for _, b := range user_group {
			if v.Tk_owner == b.Id {

				row["tk_owner"] = b.LoginName
			}
		}
		for _, d := range gw_group {
			if v.Tk_gateway_group == d.Id {

				row["tk_gateway_group"] = d.Group_name
			}
		}
		for _, e := range num_group {
			if v.Tk_num_group == e.Id {
				row["tk_num_group"] = e.Group_name
			}
		}
		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}


func (self *SeatTaskController) AjaxSave1() {
	AiSTask_id, _ := self.GetInt("id")
	if AiSTask_id == 0 {
		AiSTask := new(models.AiSeatTask)
		AiSTask.Tk_name = strings.TrimSpace(self.GetString("tk_name"))
		AiSTask.Tk_type, _ = self.GetInt("tk_type")
		AiSTask.Tk_state, _ = self.GetInt("tk_state")
		AiSTask.Tk_start_time = self.GetString("tk_start_time")
		AiSTask.Tk_end_time = self.GetString("tk_end_time")
		AiSTask.Tk_owner, _ = self.GetInt("tk_owner")
		AiSTask.Tk_gateway_group, _ = self.GetInt64("tk_gateway_group")
		AiSTask.Tk_num_group, _ = self.GetInt64("tk_num_group")
		// 检查登录名是否已经存在
		_, err := models.AiTaskGetByName1(AiSTask.Tk_name)

		if err == nil {
			self.ajaxMsg("名称已经存在", MSG_ERR)
		}
		//新增
		if _, err := models.AiSeatTaskAdd(AiSTask); err != nil {
			self.ajaxMsg(err.Error(), MSG_ERR)
		}
		self.ajaxMsg("", MSG_OK)
	}

	AiSTask, _ := models.ASeatTaskGetById(AiSTask_id)
	//修改

	AiSTask.Tk_name = strings.TrimSpace(self.GetString("tk_name"))
	AiSTask.Tk_type, _ = self.GetInt("tk_type")
	AiSTask.Tk_state, _ = self.GetInt("tk_state")
	AiSTask.Tk_start_time = self.GetString("tk_start_time")
	AiSTask.Tk_end_time = self.GetString("tk_end_time")
	AiSTask.Tk_owner, _ = self.GetInt("tk_owner")
	AiSTask.Tk_gateway_group, _ = self.GetInt64("tk_gateway_group")
	AiSTask.Tk_num_group, _ = self.GetInt64("tk_num_group")

	if err := AiSTask.Update(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *SeatTaskController) AjaxDel1() {

	AiTask_id, _ := self.GetInt("id")
	AiTask, _ := models.AiSeatTaskGetById(AiTask_id)

	if err := AiTask.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}